package cn.icanci.loopstack.bic.web.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.*;

import cn.icanci.loopstack.bic.biz.service.UseCaseService;
import cn.icanci.loopstack.bic.common.model.TextValue;
import cn.icanci.loopstack.bic.common.model.base.UseCaseVO;
import cn.icanci.loopstack.bic.common.result.R;
import cn.icanci.loopstack.bic.web.form.UseCaseQueryForm;
import cn.icanci.loopstack.bic.web.mapper.UseCaseWebMapper;
import cn.icanci.loopstack.bic.web.model.UseCase;

/**
 * @author icanci
 * @since 1.0 Created in 2023/09/10 20:03
 */
@RestController
@SuppressWarnings("all")
@RequestMapping("/bicAdmin/useCase")
public class UseCaseController {
    @Resource
    private UseCaseWebMapper useCaseWebMapper;
    @Resource
    private UseCaseService   useCaseService;

    // ================================ CRUD ================================
    @PostMapping("query")
    public R query(@RequestBody UseCaseQueryForm form) {
        return R.builderOk()
            .data("queryPage", useCaseService.queryPage(useCaseWebMapper.web2vo(form.getUseCase()), form.getPaginator().getCurrentPage(), form.getPaginator().getPageSize()))
            .build();
    }

    @PostMapping("save")
    public R save(@RequestBody UseCase useCase) {
        useCaseService.save(useCaseWebMapper.web2vo(useCase));
        return R.builderOk().build();
    }

    @GetMapping("validateName/{systemUuid}/{name}")
    public R validateName(@PathVariable("systemUuid") String systemUuid, @PathVariable("name") String name) {
        UseCaseVO useCaseVO = useCaseService.queryBySystemUuidAndName(systemUuid, name);
        return R.builderOk().data("result", useCaseVO == null).build();
    }

    @GetMapping("validateKey/{systemUuid}/{key}")
    public R validateKey(@PathVariable("systemUuid") String systemUuid, @PathVariable("key") String key) {
        UseCaseVO useCaseVO = useCaseService.queryBySystemUuidAndKey(systemUuid, key);
        return R.builderOk().data("result", useCaseVO == null).build();
    }
    // ================================ Load Selectors ================================

    @GetMapping("loadSelector/{systemUuid}")
    public R loadSelector(@PathVariable("systemUuid") String systemUuid) {
        List<TextValue> textValues = useCaseService.loadSelector(systemUuid);
        return R.builderOk().data("textValues", textValues).build();
    }

    @GetMapping("loadAllSelector")
    public R loadAllSelector() {
        List<TextValue> textValues = useCaseService.loadAllSelector();
        return R.builderOk().data("textValues", textValues).build();
    }
}
